from django import forms
from fetchemApp.models import *
from datetime import datetime

class DatePickerWidget(forms.DateInput):
    def __init__(self, placeholder, **kwargs):
        super(forms.DateInput, self).__init__(attrs={"class": "dateinput span2", "placeholder": placeholder}, **kwargs)
        self.format = '%d/%m/%Y'
    class Media:
        css = {"all":("http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/themes/redmond/jquery-ui.css",)}
        js = ("http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js",
              "http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/jquery-ui.min.js",)

class TimePickerWidget(forms.TimeInput):
    def __init__(self, placeholder, **kwargs):
        super(forms.TimeInput, self).__init__(attrs={"class": "timepicker-default span2", "placeholder": placeholder}, **kwargs)
        self.format = '%I:%M %p'

class loginForm(forms.Form):
    Email = forms.EmailField(widget=forms.TextInput(attrs={'placeholder':'Email'}))
    Password = forms.CharField(widget=forms.PasswordInput(attrs={'placeholder':'Password'}))
    Remember = forms.BooleanField(required=False, label='Remember me', widget=forms.CheckboxInput)

class newCourseForm(forms.Form):
    CourseNo = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Course Number', 'class': 'span3'}))
    Name = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Course Name', 'class': 'span3'}))
    SubEmail = forms.EmailField(widget=forms.TextInput(attrs={'placeholder': 'Submission Email', 'class': 'span3'}))
    SubPassword = forms.CharField(widget=forms.PasswordInput(attrs={'placeholder':'Submission Email Password', 'class': 'span3'}))
    SubPasswordConfirm = forms.CharField(widget=forms.PasswordInput(attrs={'placeholder':'Confirm Password', 'class': 'span3'}))
    TAName = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'TA Name', 'class': 'span2'}), required=False)
    TAEmail = forms.EmailField(widget=forms.TextInput(attrs={'placeholder': 'TA Email', 'class': 'span2'}), required=False)
    Year = forms.ChoiceField(widget=forms.Select(attrs={'class': 'span2'}), choices=(('2012', '2012'), ('2013', '2013')))
    Semester = forms.ChoiceField(widget=forms.Select(attrs={'class': 'span2'}), choices=(('Winter', 'Winter'), ('Monsoon', 'Monsoon')))
    ExtraEmailCounter = forms.IntegerField()
    ExcelSheet = forms.FileField()
    
class courseSettingsForm(forms.Form):
    CourseNo = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Course Number', 'class': 'span3'}))
    Name = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Course Name', 'class': 'span3'}))
    SubEmail = forms.EmailField(widget=forms.TextInput(attrs={'placeholder': 'Submission Email', 'class': 'span3'}))
    SubPassword = forms.CharField(widget=forms.PasswordInput(attrs={'placeholder':'Submission Email Password', 'class': 'span3'}))
    Semester = forms.ChoiceField(widget=forms.Select(attrs={'class': 'span2'}), choices=(('Winter', 'Winter'), ('Monsoon', 'Monsoon')))
    Year = forms.ChoiceField(widget=forms.Select(attrs={'class': 'span2'}), choices=(('2012', '2012'), ('2013', '2013')))
    ExtraEmailCounter2 = forms.IntegerField()
    ExtraStudentEmailCounter = forms.IntegerField()
    ExcelSheet = forms.FileField(required=False)
    
    def __init__(self, *args, **kwargs):
        course_object = kwargs.pop('course_object', None)
        super(courseSettingsForm, self).__init__(*args, **kwargs)
        if(course_object!=None):
            self.fields['CourseNo'] = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Course Number', 'class': 'span3'}), initial = course_object.CourseNo)
            self.fields['Name'] = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Course Name', 'class': 'span3'}), initial = course_object.Name)
            self.fields['SubEmail'] = forms.EmailField(widget=forms.TextInput(attrs={'placeholder': 'Submission Email', 'class': 'span3'}), initial = course_object.SubEmail)
            self.fields['SubPassword'] = forms.CharField(widget=forms.PasswordInput(attrs={'placeholder':'Submission Email Password', 'class': 'span3'}), initial = course_object.SubPassword)
            self.fields['Semester'] = forms.ChoiceField(widget=forms.Select(attrs={'class': 'span2'}), choices=(('Winter', 'Winter'), ('Monsoon', 'Monsoon')), initial = course_object.Semester)
            self.fields['Year'] = forms.ChoiceField(widget=forms.Select(attrs={'class': 'span2'}), choices=(('2012', '2012'), ('2013', '2013')), initial = course_object.Year)
    
class newAssignmentForm(forms.Form):
    SubjectLine = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Subject Line', 'class': 'span3'}))
    StartDate = forms.DateField(widget=DatePickerWidget(placeholder='Start Date'))
    StartTime = forms.TimeField(widget=TimePickerWidget(placeholder='Start Time'), input_formats=['%I:%M %p'])
    EndDate = forms.DateField(widget=DatePickerWidget(placeholder='End Date'))
    EndTime = forms.TimeField(widget=TimePickerWidget(placeholder='End Time'), input_formats=['%I:%M %p'])
    noQuestions = forms.IntegerField(widget=forms.TextInput(attrs={'placeholder': 'Number of Questions', 'value':1}))
    MaxMarksNew = forms.IntegerField(widget=forms.TextInput(attrs={'placeholder': 'Maximum Marks', 'class':'span3'}))
            
class assignmentSettingsForm(forms.Form):
    StartDateSettings = forms.DateField(widget=DatePickerWidget(placeholder='Start Date'))
    StartTimeSettings = forms.TimeField(widget=TimePickerWidget(placeholder='Start Time'), input_formats=['%I:%M %p'])
    EndDateSettings = forms.DateField(widget=DatePickerWidget(placeholder='End Date'))
    EndTimeSettings = forms.TimeField(widget=TimePickerWidget(placeholder='End Time'), input_formats=['%I:%M %p'])
    MaxMarks = forms.IntegerField(widget=forms.TextInput(attrs={'placeholder': 'Maximum Marks', 'class':'span3'}))
    
    def __init__(self, *args, **kwargs):
        assn_object = kwargs.pop('assn_object', None)
        super(assignmentSettingsForm, self).__init__(*args, **kwargs)
        if(assn_object!=None):
#             self.fields['StartDate'] = forms.DateField(widget=DatePickerWidget(placeholder='Start Date'))
#            self.fields['StartTime'] = forms.TimeField(widget=TimePickerWidget(placeholder='Start Time'), input_formats=['%I:%M %p'])
#            self.fields['EndDate'] = forms.DateField(widget=DatePickerWidget(placeholder='End Date'))
#            self.fields['EndTime'] = forms.TimeField(widget=TimePickerWidget(placeholder='End Time'), input_formats=['%I:%M %p'])
            self.fields['MaxMarks'] = forms.IntegerField(widget=forms.TextInput(attrs={'placeholder': 'Maximum Marks', 'class':'span3'}), initial=assn_object.MaxMarks)

class submissionForm(forms.Form):
    
    def __init__(self, *args, **kwargs):
        subObject = kwargs.pop('subObject', None)
        AssnObject = kwargs.pop('AssnObject', None)
        alreadyGraded = kwargs.pop('alreadyGraded', False)
        super(submissionForm, self).__init__(*args, **kwargs)
        if(AssnObject==None or subObject==None):
            return None 
        if(alreadyGraded==False):
            for i in AssnObject.question_set.filter(ParentQuestion=None):
                self.fields['Q' + str(i.QNo)] = forms.FloatField(widget=forms.TextInput(attrs={'class': 'span1', 'value': 0}))
            for i in AssnObject.question_set.all().exclude(ParentQuestion=None):
                self.fields['Q' + str(i.ParentQuestion.QNo) + '.' + str(i.QNo)] = forms.FloatField(widget=forms.TextInput(attrs={'class': 'span1', 'value': 0}))
            self.fields['Comments'] = forms.CharField(widget=forms.Textarea(attrs={'placeholder':'Comment', 'value':'', 'rows':2}), required=False)
            self.fields['BPType'] = forms.ChoiceField(widget=forms.Select(attrs={'class': 'span3'}), choices=(('P', 'Bonus Percentage'), ('M', 'Bonus Marks')))
            #self.initial['BPType'] = self.fields[]
            self.fields['BPValue'] = forms.FloatField(widget=forms.TextInput(attrs={'class': 'span1', 'value': 0}))

        else:
            for i in AssnObject.question_set.filter(ParentQuestion=None):
                self.fields['Q' + str(i.QNo)] = forms.FloatField(widget=forms.TextInput(attrs={'class': 'span1', 'value': QuestionGrade.objects.get(submission=subObject, question=i).Marks}))
            for i in AssnObject.question_set.all().exclude(ParentQuestion=None):
                self.fields['Q' + str(i.ParentQuestion.QNo) + '.' + str(i.QNo)] = forms.FloatField(widget=forms.TextInput(attrs={'class': 'span1', 'value': QuestionGrade.objects.get(submission=subObject, question=i).Marks}))
            self.fields['Comments'] = forms.CharField(widget=forms.Textarea(attrs={'placeholder':'Comment', 'rows':2}), required=False)
            self.initial['Comments'] = subObject.Comment
            self.fields['BPType'] = forms.ChoiceField(widget=forms.Select(attrs={'class':'span3'}), choices=(('P', 'Percentage'), ('M', 'Marks')))
            #TODO-Set BPType Initial
            self.fields['BPValue'] = forms.FloatField(widget=forms.TextInput(attrs={'class': 'span1','value': subObject.BonusPenaltyValue}))
        return None